<?php

/**
 *    文章管理控制器
 *
 *    @author    Hyber
 *    @usage    none
 */
class CourseqaApp extends BackendApp
{
	var $_courseqa_mod;

	function __construct()
	{
		$this->CourseqaApp();
	}

	function CourseqaApp()
	{
		parent::BackendApp();

		$this->_courseqa_mod =& m('courseqa');
	}

	/**
	 *    文章索引
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function index()
	{
        $search_options = array(
            'course_name'   => Lang::get('course_name'),
            'question'   => Lang::get('question'),
            'cm.user_name'   => Lang::get('user_name'),
        );
        /* 默认搜索的字段是店铺名 */
        $field = 'course_name';
        array_key_exists($_GET['field'], $search_options) && $field = $_GET['field'];
        $conditions = $this->_get_query_conditions(array(array(
            'field' => $field,       //按用户名,店铺名,支付方式名称进行搜索
            'equal' => 'LIKE',
            'name'  => 'search_name',
        ),array(
            'field' => 'add_time',
            'name'  => 'add_time_from',
            'equal' => '>=',
            'handler'=> 'gmstr2time',
        ),array(
            'field' => 'add_time',
            'name'  => 'add_time_to',
            'equal' => '<=',
            'handler'   => 'gmstr2time_end',
        ),
        ));
		$page   =   $this->_get_page(10);   //获取分页信息
        $sql_count = 'SELECT COUNT(*) FROM cw_courseqa AS cq
                LEFT JOIN cw_course AS cc ON cc.course_id = cq.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = cq.uid
                WHERE 1=1 ORDER BY cq.add_time DESC';
        $res_count = $this->_courseqa_mod->getOne($sql_count);

        $sql = 'SELECT cq.*,cc.name AS course_name,cm.user_name,cm.real_name FROM cw_courseqa AS cq
                LEFT JOIN cw_course AS cc ON cc.course_id = cq.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = cq.uid
                WHERE 1=1 ORDER BY cq.add_time DESC LIMIT '.$page['limit'];
        $courseqas = $this->_courseqa_mod->getAll($sql);
		$page['item_count']=$res_count;   //获取统计数据
		$this->_format_page($page);
        $this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
            'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
		$this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('search_options', $search_options);
		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('courseqas', $courseqas);
		$this->display('courseqa.index.html');
	}
	/**
	 *    编辑文章
	 *
	 *    @author    Hyber
	 *    @return    void
	 */
	function edit()
	{
		$courseqa_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		if (!$courseqa_id)
		{
			$this->show_warning('no_such_course');
			return;
		}
		if (!IS_POST)
		{
            $sql = 'SELECT cq.*,cc.name AS course_name,cm.user_name,cm.real_name FROM cw_courseqa AS cq
                LEFT JOIN cw_course AS cc ON cc.course_id = cq.course_id
                LEFT JOIN cw_member AS cm ON cm.user_id = cq.uid
                WHERE qa_id = '.$courseqa_id;
            $courseqa = $this->_courseqa_mod->getRow($sql);
            $this->import_resource(array('script' => 'jquery.plugins/jquery.validate.js,change_upload.js'));
            $template_name = $this->_get_template_name();
            $style_name    = $this->_get_style_name();
            $this->assign('build_editor', $this->_build_editor(array(
                'name' => 'content',
                'content_css' => SITE_URL . "/themes/mall/{$template_name}/styles/{$style_name}/css/ecmall.css"
            )));
            $this->assign('courseqa', $courseqa);
            $this->display('courseqa.form.html');
		}
		else
		{
			
			$data = array();
			$data['question']          =   $_POST['question'];
			$data['is_show']      =   $_POST['is_show'];
			$data['answer']        =   $_POST['answer'];

			$rows=$this->_courseqa_mod->edit($courseqa_id, $data);
			if ($this->_courseqa_mod->has_error())
			{
				$this->show_warning($this->_courseqa_mod->get_error());
				return;
			}

			$this->show_message('edit_courseqa_successed',
                'back_list',        'index.php?app=courseqa',
                'edit_again',    'index.php?app=courseqa&amp;act=edit&amp;id=' . $courseqa_id);
		}
	}

	//异步修改数据
	function ajax_col()
	{
		$id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$column = empty($_GET['column']) ? '' : trim($_GET['column']);
		$value  = isset($_GET['value']) ? trim($_GET['value']) : '';
		$data   = array();

		if (in_array($column ,array('is_show')))
		{
			$data[$column] = $value;
			$this->_courseqa_mod->edit($id, $data);
			if(!$this->_courseqa_mod->has_error())
			{
				echo ecm_json_encode(true);
			}
		}
		else
		{
			return ;
		}
		return ;
	}
	function drop()
	{
		$courseqa_ids = isset($_GET['id']) ? trim($_GET['id']) : '';
		if (!$courseqa_ids)
		{
			$this->show_warning('no_such_course');

			return;
		}
		$courseqa_ids=explode(',', $courseqa_ids);
		$message = 'drop_ok';
		foreach ($courseqa_ids as $key=>$courseqa_id){
			$courseqa=$this->_courseqa_mod->find(intval($courseqa_id));
			$courseqa=current($courseqa);
			if($courseqa['code']!=null)
			{
				unset($courseqa_ids[$key]);  //有部分是系统文章 过滤掉
				$message = 'drop_ok_system_courseqa';
			}
			else
			{

			}
		}
		if (!$courseqa_ids)
		{
			$message = 'system_courseqa'; //全是系统文章
			$this->show_warning($message);

			return;
		}
		if (!$this->_courseqa_mod->drop($courseqa_ids))    //删除
		{
			$this->show_warning($this->_courseqa_mod->get_error());
			return;
		}

		$this->show_message($message);
	}
}